﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using BOL;

namespace Url_CMS_UI.Areas.Common.Controllers
{
    public class BrowseURLController : Controller
    {
        private UrlBs _objBs; 

        public BrowseURLController()
        {
            _objBs = new UrlBs();

        }
        // GET: Common/BrowseURL
        public ActionResult Index( string sortOrder,string sortBy,string Page)
        {
            ViewBag.SortOrder = sortOrder;
            ViewBag.SortBy = sortBy;
            var urls = _objBs.GetAll().Where(x=>x.IsApproved=="A").ToList();

            switch (sortBy)
            {
                case "Title":
                    switch (sortOrder)
                    {
                        case "Asc":
                            urls = urls.OrderBy(x => x.UrlTitle).ToList();
                            break;
                        case "Desc":
                            urls = urls.OrderByDescending(x => x.UrlTitle).ToList();
                            break;
                    }
                    break;

                case "Category":
                    switch (sortOrder)
                    {
                        case "Asc":
                            urls = urls.OrderBy(x => x.tbl_Category.CategoryName).ToList();
                            break;
                        case "Desc":
                            urls = urls.OrderByDescending(x => x.tbl_Category.CategoryName).ToList();
                            break;
                    }
                    break;

                case "Url":
                     switch (sortOrder)
                    {
                        case "Asc":
                            urls = urls.OrderBy(x => x.Url).ToList();
                            break;
                        case "Desc":
                            urls = urls.OrderByDescending(x => x.Url).ToList();
                            break;
                    }
                    break;

                case "UrlDesc":
                     switch (sortOrder)
                    {
                        case "Asc":
                            urls = urls.OrderBy(x => x.UrlDesc).ToList();
                            break;
                        case "Desc":
                            urls = urls.OrderByDescending(x => x.UrlDesc).ToList();
                            break;
                    }
                    break;
                    
                default:
                    urls = urls.OrderBy(x => x.UrlTitle).ToList();
                    break;

            }

           
            ViewBag.TotalPages = Math.Ceiling(_objBs.GetAll().Count(x => x.IsApproved == "A") / 10.0);

            int page = int.Parse(Page == null ? "1" : Page);
            ViewBag.Page = page;
            urls = urls.Skip((page - 1)*10).Take(10).ToList();



            return View(urls);
        }
    }
}